# coding : utf-8

import numpy as np
import configparser
import os
from ta import TA


class MyParseIni(configparser.ConfigParser):
    def as_dict(self):
        d = dict(self._sections)
        for k in d:
            d[k] = dict(d[k])
        return d        
        
if __name__ == "__main__":

    #字段名称
    names=['Date','Time','Id_no','Acc_no','Acc_name','Part_acc_flag','Part_acc_no','Part_acc_name','Lend_flag','Tsf_flag','Org_amt','Balance','Ticd','Settle_type','Reverse_flag','Purpose']
    dtype={'Date':str, 'Time':str, 'Id_no':str, 'Acc_no':str, 'Acc_name':str, 'Part_acc_flag':str, 'Part_acc_no':str, 'Part_acc_name':str, 'Lend_flag':str, 'Tsf_flag':str, 'Org_amt':np.float64, 'Balance':np.float64, 'Ticd':str, 'Settle_type':str, 'Reverse_flag':str, 'Purpose':str}

    
    names1=['Date','Time','Id_no','Acc_no','Part_acc_no','Lend_flag','Org_amt','Balance']
    dtype1={'Date':str,'Time':str,'Id_no':str,'Acc_no':str,'Part_acc_no':str,'Lend_flag':str,'Org_amt':np.float64,'Balance':np.float64}
    usecols1=[0,1,2,3,6,8,11,12]
    
    #取得配置文件
    conf = MyParseIni()
    ini_file_path = os.path.join(os.getcwd(), 'config.ini')
    conf.read(ini_file_path, encoding="utf-8")
    config = conf.as_dict()
    config['FILE']['header']=int(config['FILE']['header'])
    config['FILE']['chunksize']=int(config['FILE']['chunksize'])
    config['FIGURE']['scatter_alpha']=float(config['FIGURE']['scatter_alpha'])
    config['FIGURE']['scatter_shape']=float(config['FIGURE']['scatter_shape'])
    config['FIGURE']['plot_alpha']=float(config['FIGURE']['plot_alpha'])
    config['FIGURE']['plot_width']=float(config['FIGURE']['plot_width'])
    config['FUNCTION']['length_acc']=int(config['FUNCTION']['length_acc'])
    print(config)
    
    ta = TA(config)
    
    if config['FUNCTION']['all']=='Y':
        accounts = set()
        ta.get_acc_no_before()
        accounts = ta.set_acc
    else:
        accounts = list()
        f=open(config['FUNCTION']['accfilepath'], encoding='utf-8')
        for line in f:
            accounts.append(line.strip())
        f.close()
        
    for acc in accounts:
        print('账号：',acc)
        print('提取数据')
        # 读取全部数据列
        #ta.get_data_chunk(names=names, dtype=dtype, acc_no=acc)
        # 按指定列号usecols读取数据
        ta.get_data_chunk_columns(names=names1, dtype=dtype1, usecols=usecols1, acc_no=acc)
        if ta.df.empty:  #判断数据集df是否为empty dataframe
            print("【错误】未找到数据")
            continue
        print('清洗数据')
        ta.flush_data()
        print('构造数据')
        ta.construct_data()
        print('数据排序')
        ta.sort_data()
        print('特征计算')
        ta.feature1()
        ta.feature2()
        
        print("绘制图形")
        df = ta.extract_df(acc)
        ta.generate_figure(df, acc)
